#include "LayoutPageItem.h"
#include "oled.h"
#include <string.h>
#include <stdio.h>

namespace MircoUi {
    
    // 绘制数字项
    sVec2 NumItem::draw(sVec2 ap) {
        sVec2 s;        
        char temp_buffer[32] = {0};
        uint8 len;
        len = sprintf(temp_buffer, format, data);
        OLED_ShowString(ap.x, ap.y, temp_buffer, h, mode);
        if(h==8){
            s.x = 6*len ;
        } else {
            s.x = h/2*len;
        }
        s.y = h;        
        return s;        
    }
    
    // 设置数字项的数据
    void NumItem::initData(const char* format, uint8 len) {
        this->format = format;   
        num_len = len;       
    }
}

namespace MircoUi {
    
    sVec2 StringItem::draw(sVec2 ap){
        sVec2 s;
        OLED_ShowString(ap.x, ap.y, (char*)chr, h, mode);
        uint8 len = strlen(chr); 
        if(h==8){
            s.x = 6*len;
        } else {
            s.x = h/2*len;
        }
        s.y = h;        
        return s;        
    }
    void StringItem::setData(const char* str) {
        chr = str;
        changed(); // 设置数据后标记为需要更新
    } 
}

namespace MircoUi {
    sVec2 ImageItem::draw(sVec2 ap) {
        OLED_ShowPicture(ap.x, ap.y, size.x, size.y,image,mode);
        return size;
    }
}
